{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#范围缩放， 以列为单位，每列的最小值未0，最大值为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import sklearn.preprocessing as sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = np.array([[1.0,2.0,3.0],\n",
    "                   [4.0,5.0,9.0],\n",
    "                   [7.0,8.0,11.0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.   0.   0.  ]\n",
      " [0.5  0.5  0.75]\n",
      " [1.   1.   1.  ]]\n"
     ]
    }
   ],
   "source": [
    "mms = sample.copy()\n",
    "for col in mms.T:\n",
    "    col_min = col.min()\n",
    "    col_max = col.max()\n",
    "    col -= col_min\n",
    "    col /= (col_max - col_min)\n",
    "    \n",
    "print(mms)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 利用sklearn中的接口实现范围缩放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.   0.   0.  ]\n",
      " [0.5  0.5  0.75]\n",
      " [1.   1.   1.  ]]\n"
     ]
    }
   ],
   "source": [
    "#构建范围缩放器\n",
    "mms = sp.MinMaxScaler(feature_range=(0,1))\n",
    "res = mms.fit_transform(sample)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
